﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

using EnvDTE;
using EnvDTE100;
using VSLangProj;
using VslangProj100;

namespace Artisteer_Project_Converter.VisualStudioApi
{
    public class VsProject
    {
        private Project project = null;
        private VSProject vsproject = null;

        private string name = "";

        /// <summary>
        /// Create project instance
        /// </summary>
        /// <param name="solution">Project object</param>
        internal VsProject(Project project)
        {
            this.project = project;
            this.vsproject = (VSProject)project.Object;
        }

        /// <summary>
        /// Add file to project
        /// </summary>
        /// <param name="file">File to be added to project</param>
        /// <returns>Project item</returns>
        public VsProjectItem AddFile(string filename)
        {
            while (true)
            {
                try
                {
                    return new VsProjectItem(project.ProjectItems.AddFromFile(filename));
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Add folder to project
        /// </summary>
        /// <param name="file">Folder to be added to project</param>
        /// <returns>Project item</returns>
        public VsProjectItem addFolder(string folderName)
        {
            while (true)
            {
                try
                {
                    return new VsProjectItem(project.ProjectItems.AddFolder(folderName));
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Add a reference
        /// </summary>
        /// <param name="filename">File name</param>
        public void AddReference(string filename)
        {            
            while (true)
            {
                try
                {
                    vsproject.References.Add(filename);
                    break;
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Save project
        /// </summary>
        public void Save()
        {
            while (true)
            {
                try
                {
                    project.Save();
                    break;
                }
                catch (COMException ex)
                {
                    if (ex.ErrorCode == -2147418111)
                        System.Threading.Thread.Sleep(100);
                    else
                        throw ex;
                }
            }
        }

        /// <summary>
        /// Save project as
        /// </summary>
        public void SaveAs(string filename)
        {
            project.SaveAs(filename);
        }

        /// <summary>
        /// Project name
        /// </summary>
        public string Name
        {
            get
            {
                if (name == "")
                {
                    while (true)
                    {
                        try
                        {
                            return project.Name;
                        }
                        catch (COMException ex)
                        {
                            if (ex.ErrorCode == -2147418111)
                                System.Threading.Thread.Sleep(100);
                            else
                                throw ex;
                        }
                    }
                }

                return name;
            }
        }
    }
}
